package org.example.subject3;

import java.util.HashMap;
import java.util.Map;

/**
 * @author brant
 */
public class Solution {

    public static void main(String[] args) {
        Solution solution = new Solution();
        String[] strings = {
                "abcabcbb",
                "bbbbb",
                "pwwkew",
                "",
                "ckilbkd",

        };
        for (String string : strings) {
            int len = solution.lengthOfLongestSubstring(string);
            System.out.println("len: " + len);
        }
    }

    public int lengthOfLongestSubstring(String s) {
        int maxLen = 0, tempLen = 0;
        Map<Character, Integer> map = new HashMap<>(0);
        for(int i = 0; i < s.length(); i++) {
            Integer lastIndex = map.get(s.charAt(i));
            map.put(s.charAt(i), i);
            if (lastIndex == null) {
                tempLen++;
            } else if (lastIndex == i - 1) {
                tempLen = 1;
            } else {
                tempLen = Math.min(i - lastIndex, tempLen + 1);
            }
            if (maxLen < tempLen) {
                maxLen = tempLen;
            }
        }
        return maxLen;
    }

}
